package org.prime;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

//rename file to SubProcess.java
public class SubProcess extends Thread implements timeTaken {

        private SortedSet<LongInteger> primePackage; // otrzymywany pakiet danych
        private SortedSet<LongInteger> primePackageReturn; // zwracany pakiet danych

        private Iterator<LongInteger> primePackageIterator;

        public LongInteger primePackageSize;
        public LongInteger primePackageReturnSize;

        public int primeNumberSize = Management.primeNumberSize;

        private static int totalThreads = 0;
        public int id;

        private long startTime;
        private long endTime;
        public long totalTime;
        public long intTime;

        public LongInteger bufferSize; // ilość liczb pierwszych do wyliczenia
        public LongInteger beginningValue; // liczba od której liczymy liczbę
                                                                                // pierwszą // sprawdz czy parzysta czy
                                                                                // nieparzysta, funkcje modulo(BValue) i
                                                                                // BValue.is0()

        public SubProcess() {
                id = SubProcess.totalThreads++;
                primePackage = new TreeSet<LongInteger>();
                LongInteger zero = new LongInteger();
                zero.set(0);
                LongInteger liczba50 = new LongInteger();
               LongInteger primePackageSize=new LongInteger();
               primePackageSize.set(zero);
               LongInteger primePackageReturnSize=new LongInteger();
               primePackageReturnSize.set(liczba50) ;
                primePackageIterator = primePackage.iterator();
                this.intTime = 0;
                this.bufferSize = liczba50;
        }

        @Override
        public void run() {
                
                try {
                        this.calculate();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                // calculate() method here
                /*
                 * try{ this.calculate(); }catch(Exception e){
                 * 
                 * System.out.println(e.getMessage()); e.printStackTrace();
                 * while(this.primePackage.size()==0){} try { this.calculate(); } catch
                 * (Exception e1) { // TODO Auto-generated catch block
                 * e1.printStackTrace(); } }
                 */
                // at the end of run()
        }

        private void calculate() throws Exception { // old start()
        	
        	
        	ArrayList<Integer> lista = new ArrayList();
//    		int ilośćLiczbPierwszych = 3000;
//    		int zakres=15000;
//    		for (int i=3;i<=zakres;i+=2){
//    			
//    			int licznik=0;
//    		
//    		}
    		for (int i=0;i<lista.size();i++)
    			System.out.println(lista.get(i));

                if (primePackage.size() == 0)
                        throw new Exception("Empty prime numbers Package...");

                LongInteger liczba2 = new LongInteger();
                liczba2.set(2);
                if (this.beginningValue == null) {
                        this.beginningValue= new LongInteger();
                        this.beginningValue.set( primePackage.last());
                        this.beginningValue.set( liczba2.add2(this.beginningValue));
                }
                primePackageReturn = new TreeSet<LongInteger>();
                this.primeNumberSize = 0;
                boolean flag = false;
                if (this.id> 0)
                        System.out.println("inny watek");
                for (; this.primeNumberSize < Integer.parseInt(bufferSize.toString()) ; ) {
                //      System.out.println(this.id);
                                flag = false;
                                primePackageIterator = this.primePackage.iterator();
                                while (primePackageIterator.hasNext()) {
                                        LongInteger valu = null;
                                        while (true)
                                                try{
                                                        valu = primePackageIterator.next();
                                                        break;
                                                }
                                                catch(Exception e)
                                                {
                                                        
                                                }
                                        
                                        
                                        LongInteger zero = new LongInteger();
                                        zero.set(0);
                                        if (this.beginningValue.mod(valu).equals(zero)){
                                                flag = true;
                                                break;
                                        }
                                        if  (valu.greater(beginningValue.sqrt()))
                                        {
                                                break;
                                        }
                                }
                                if (!flag) {
                                			int licznik=0;
                                		
                                	for (int j=2;j<Integer.parseInt(this.beginningValue.toString());j++){
                        				if (Integer.parseInt(this.beginningValue.toString())%j==0)
                        					licznik++;
                        			}	
                        			if (licznik==0)
                        			{
                                	
                                                this.primePackageReturn.add(this.beginningValue);
                                                this.primeNumberSize++;
                                                if (this.primeNumberSize == Integer.parseInt(this.bufferSize.toString()))
                                                        break;
                                                System.out.println(this.id +" added value: "+beginningValue.toString());
                                }}
                                this.beginningValue = this.beginningValue.add2(liczba2);
                        
                        
                }
        //      System.out.println("Thread stop calculating...");
        //      System.out.println(this.primeNumberSize+ "  ==  "+ this.bufferSize);
        
                // calculate next buffersize numbers
                // primePackageReturn = new ....;
                // primePackageReturn.addAll(collecion of calculated values)
        }

        public SortedSet<LongInteger> getPrimePackageReturn() {
                // TODO add this package to primePackage
                return primePackageReturn;
        }

        private void setBufferSize(LongInteger value) {
                this.bufferSize = value;
        }

        public void setPrimePackage(SortedSet<LongInteger> primePackage) {
                this.primePackage = primePackage;
        }

        @Override
        public Long returnTime(Long begining, Long ending) {

                return ending - begining;
        }
}